import PyPDF4


def merge_pdfs(pdf_list, output_pdf):
    merger = PyPDF4.PdfFileMerger()

    for pdf in pdf_list:
        with open(pdf, 'rb') as file:
            merger.append(file)

    with open(output_pdf, 'wb') as output_file:
        merger.write(output_file)

def remove_last_page(input_pdf_path, output_pdf_path):  # 删除最后一页PDF
    # 打开PDF文档
    with open(input_pdf_path, 'rb') as input_pdf:
        reader = PyPDF4.PdfFileReader(input_pdf)
        # 创建一个新的PDF写入器
        writer = PyPDF4.PdfFileWriter()
        # 遍历所有页面，除了最后一页
        for page_num in range(reader.numPages - 1):
            page = reader.getPage(page_num)
            writer.addPage(page)
        # 将新文档写入文件
        with open(output_pdf_path, 'wb') as output_pdf:
            writer.write(output_pdf)


# if __name__ == "__main__":
#     # 替换为你的PDF文件路径列表
#     # pdf_files = [r'1\1.pdf', r'1\2.pdf', r'1\3.pdf', r'1\4.pdf', r'1\5.pdf']
#     pdf_files = [r'1.pdf', r'2.pdf']
#     # 替换为你想要输出的合并后的PDF文件路径
#     output_file = 'merged_output.pdf'
#
#     merge_pdfs(pdf_files, output_file)
#     print(f"PDFs merged into {output_file}")


if __name__ == '__main__':
    # 使用示例
    input_pdf_path = 'merged_output.pdf'  # 输入PDF文件路径
    output_pdf_path = 'output.pdf'  # 输出PDF文件路径（不包含最后一页）
    remove_last_page(input_pdf_path, output_pdf_path)
